package com.usernet.product.server;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.usernet.product.dao.CommentDao;
import com.usernet.product.entity.Comment;
import com.usernet.product.entity.Users;
import com.usernet.product.tools.JSONTools;
import com.usernet.product.utils.DateUtils;

public class CommentService {

	private CommentDao commentDao = new CommentDao();

	/**
	 * 获取所有评论
	 */
	public String getAllComment(HttpServletRequest request) {
		String ret = null;
		try {
			String softId = request.getParameter("softId");
			int size = Integer.parseInt(request.getParameter("pageSize"));
			int no = Integer.parseInt(request.getParameter("pageNo"));
			List<Comment> list = commentDao.getAllBySoftId(Integer
					.parseInt(softId), size, no);
			int maxSize = 0;
			list = commentDao
					.getAllBySoftId(Integer.parseInt(softId), size, no);
			if (no == 1) {
				maxSize = commentDao.getCountBySoftId(Integer.parseInt(softId));
			}
			StringBuffer buf = new StringBuffer();
			buf.append("[");
			for (int i = 0; i < list.size(); i++) {
				Comment comment = list.get(i);
				buf.append("{");
				buf.append("niceName:\"");
				Users user = comment.getUsers();
				if (user != null) {
					buf.append(user.getNiceName());
				} else {
					buf.append("手机游客");
				}
				buf.append("\",info:\"");
				buf.append(JSONTools.string2Json(comment.getInfo()));
				buf.append("\",score:");
				buf.append(comment.getScore());
				buf.append(",time:\"");
				buf.append(DateUtils.timestampToString(comment.getTime()));
				buf.append("\"}");
				if (i < list.size() - 1) {
					buf.append(",");
				}
			}
			buf.append("],size:");
			buf.append(maxSize);
			ret = MarketUtils.getResJson(buf.toString());

		} catch (Exception ex) {
			ex.printStackTrace();
			ret = MarketUtils.getThrowJson();
		}
		return ret;
	}
}
